package com.example.admindemo.controller;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.UUID;
import com.example.admindemo.entity.Result;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RequestMapping("/files")
@RestController
@CrossOrigin
public class FileController {
    @PostMapping("upload")
    public Result upload(MultipartFile file) throws IOException {
        System.out.println(file);
        String originalFilename = file.getOriginalFilename();
        String root =UUID.randomUUID()+"-"+originalFilename;
        String dir_name = System.getProperty("user.dir");
        System.out.println(dir_name);
        FileUtil.writeBytes(file.getBytes(),dir_name+"/"+"src/main/resources/static/upload/"+root);
        String url = "localhost:9090/static/upload/"+root;
        return Result.success(url);
    }
}
